package dk.rhok.server;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import dk.rhok.model.Assessment;
import dk.rhok.model.Coordinate;
import dk.rhok.model.MapOverlay;
import dk.rhok.model.ShelterAssessmentImpl;
import dk.rhok.model.WatSanAssessmentImpl;

/**
 * Respons to web requests for map overlays
 * 
 * @author Jonas Grann <jonasgrann@gmail.com>
 * @since 0.1
 */

@WebServlet("/Server")
public class MapOverlayServlet extends HttpServlet {

	// Static demo server
	private static MapOverlayServer server = new MapOverlayServerImpl();

	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Set content type
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");

		// Get _GET parameters
		double latitude_from, longitude_from, latitude_to, longitude_to;
		String types = "all";

		try {
			types = request.getParameter("types");
			
			latitude_from = Double.parseDouble(request
					.getParameter("latitude_from"));
			longitude_from = Double.parseDouble(request
					.getParameter("longitude_from"));
			latitude_to = Double.parseDouble(request
					.getParameter("latitude_to"));
			longitude_to = Double.parseDouble(request
					.getParameter("longitude_to"));
			
		} catch (Exception e) {
			// Just return all
			latitude_from = -180;
			longitude_from = -180;
			latitude_to = 180;
			longitude_to = 180;
		}

		// Create coordinates used to get correct overlays
		Coordinate from = new Coordinate(latitude_from, longitude_from);
		Coordinate to = new Coordinate(latitude_to, longitude_to);

		// Get overlays
		types = types + ",danger_zone"; // Always display these
		List<MapOverlay> overlays = server.getOverlays(from, to, types);

		// Convert to json
		Gson json = new Gson();
		response.getWriter().write(json.toJson(overlays));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Handle post (used for adding assessments

		MapOverlayCreator creator = new MapOverlayCreatorImpl();

		MapOverlay overlay = creator.addMapOverlayFromPOST(request);
		
		if (overlay != null) {
			server.addMapOverlay(overlay);
			response.getWriter().write("Overlay added");
		} else {
			response.getWriter()
					.write("Could not add overlays, wrong type or invalid arguments");
		}
	}
}
